﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Services;
using MethodWorx.Core.DomainModel;
using MethodWorx.Core.Validation;

namespace MethodWorx.CMS.Workflow.CreateMember
{
    public class CreateMemberWorkflowComponent : WorkflowComponent<CreateMemberConfigurationController>
    {
        public CreateMemberWorkflowComponent(Plugin plugin)
            : base(plugin)
        {
        }

        public static Guid ComponentId = Guid.Parse("{FAC6CCD2-EBCF-4EC4-88F4-162716E7107D}");

        public override Guid Id
        {
            get { return ComponentId;  }
        }

        public override string Category
        {
            get { return "Membership";  }
        }

        public override string Name
        {
            get { return "Create Member";  }
        }

        public override string Description
        {
            get { return "Creates a member of the site";  }
        }

        public override string View
        {
            get { return "CreateMemberConfigurationView.ascx"; }
        }

        public override StepExecutionResult Execute(IWorkflowExecutionContext context, IWorkflowConfiguration configuration)
        {
            //
            //  ok, we can do this regardless of the validation state of the view
            var service = context.GetService<IUserService>();

            var member = service.CreateMember(
            this.ParseString(((CreateMemberConfiguration)configuration).Name, context.GetAllWorkflowValues()),
            this.ParseString(((CreateMemberConfiguration)configuration).EmailAddress, context.GetAllWorkflowValues()),
            this.ParseString(((CreateMemberConfiguration)configuration).Password, context.GetAllWorkflowValues()),
            new WorkflowValidationProvider(context));

            return StepExecutionResult.Ok(member);

        }
    }
}
